package com.mes.device.server.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.common.pojo.MESResult;
import com.mes.common.util.Constant;
import com.mes.device.mapper.DeviceMapper;
import com.mes.device.server.DeviceServer;
import com.mes.pojo.device.Device;
import com.mes.pojo.fixture.Fixture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @Author 张喆
 * @Company
 * @Description
 * @Time 2024/10/17 18:39
 */
@Service
public class DeviceServerImpl implements DeviceServer {
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public MESResult queryAll(Device device, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Device> devices = deviceMapper.selectAll(device);
        PageInfo<Device> pageInfo = new PageInfo<>(devices);
        return MESResult.success(pageInfo);
    }

    @Override
    public MESResult getExport(Device device, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Device> devices = deviceMapper.selectAll(device);
        List<Device> devList = new PageInfo<>(devices).getList();
        //判断文件夹是否存在不存在便创建文件夹
        File folder = new File("D:\\MES\\Excel");
        if (!folder.exists()){
            folder.mkdirs();
        }
        EasyExcel.write("D:\\MES\\Excel\\device.xlsx", Device.class).sheet("设备数据-" + page).doWrite(devList);
        return MESResult.success();
    }

    @Override
    public MESResult queryByDeviceId(String deviceId) {
        return MESResult.success(deviceMapper.selectByDeviceId(deviceId));
    }

    @Override
    public MESResult updateDevice(Device device) {
        Device same = deviceMapper.selectByDeviceName(device.getDeviceName());
        if (same != null && !same.getDeviceId().equals(device.getDeviceId())){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        return MESResult.success(deviceMapper.updateDevice(device));
    }

    @Override
    public MESResult deleteDevice(String deviceId) {
        Device device = deviceMapper.selectByDeviceId(deviceId);
        device.setDeviceStatus(99);
        return MESResult.success(deviceMapper.updateDevice(device));
    }

    @Override
    public MESResult addNewDevice(Device device) {
        Device same = deviceMapper.selectByDeviceName(device.getDeviceName());
        if (same != null){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        String maxDeviceId = deviceMapper.selectMaxDeviceId();
        String[] split = maxDeviceId.split("_");
        int num = Integer.parseInt(split[1]);
        num = num + 1;
        String strNum = null;
        if (num < 100 && num >= 10){
            strNum = "0" + num;
        }else if (num < 10 && num > 0){
            strNum = "00" + num;
        }else {
            strNum = num + "";
        }
        device.setDeviceId(split[0] + "_" + strNum);
        return MESResult.success(deviceMapper.insertNewDevice(device));
    }
}
